package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.expressme.openid.Authentication;

import myfamily.model.GenericPerson;
import myfamily.model.RegisteredUser;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowAuthenticationResult extends ActionSupport {
	private List<String> operations;
	private List<String> familymembers;
	private String authId;
	private String emailId;
	private String name;
	private String treeString;
	public String execute(){
		
		try{
		Map session = ActionContext.getContext().getSession();
		Authentication auth = new Authentication();
		auth = (Authentication)session.get("authentication");
		setAuthId(auth.getIdentity().split("=")[1]);
		System.out.println(getName());
		
		
		//return "showCreateTreePage";
		System.out.println(getAuthId());
		RegisteredUser ru = RegisteredUser.login(getAuthId());
		operations = new ArrayList<String>();
		operations.add("Add Child");
		operations.add("Add Parents");
		operations.add("Add Partner");
		operations.add("Add Sibling");
		operations.add("Delete Member");
		operations.add("Edit Details");
		familymembers = new ArrayList<String>();
		if(ru == null){
			System.out.println("New User Needs Registration");
			return "showCreateTreePage";
		}
		else{
			System.out.println("User Registered previously");
			System.out.println(ru.getContactDetails().getEmail());
			familymembers.clear();
			ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
			for(GenericPerson g: fm){
				familymembers.add(g.getPersonName().toString());
				
			}
			setTreeString(ru.printTree().toString());					
			return SUCCESS;
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "showLogin";
				
	}
	public String getEmailId() {
		return emailId;
	}
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthId() {
		return authId;
	}
	public void setAuthId(String authId) {
		this.authId = authId;
	}
	public String getTreeString() {
		return treeString;
	}
	public void setTreeString(String treeString) {
		this.treeString = treeString;
	}
	public List<String> getOperations() {
		return operations;
	}
	public void setOperations(List<String> operations) {
		this.operations = operations;
	}
	public List<String> getFamilymembers() {
		return familymembers;
	}
	public void setFamilymembers(List<String> familymembers) {
		this.familymembers = familymembers;
	}
}